import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QFileDialog

class ImageViewer(QWidget):
    def __init__(self):
        super().__init__()
        self.current_image = None
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        # 图片显示标签
        self.image_label = QLabel()
        self.image_label.setAlignment(Qt.AlignCenter)
        self.image_label.setMinimumSize(400, 300)
        self.image_label.setStyleSheet("border: 1px solid gray;")
        self.image_label.setText("请选择图片文件")
        
        # 控制按钮
        btn_layout = QHBoxLayout()
        open_btn = QPushButton('打开图片')
        open_multiple_btn = QPushButton('打开多张图片')
        save_as_btn = QPushButton('另存为')
        
        open_btn.clicked.connect(self.open_image)
        open_multiple_btn.clicked.connect(self.open_multiple_images)
        save_as_btn.clicked.connect(self.save_image_as)
        
        btn_layout.addWidget(open_btn)
        btn_layout.addWidget(open_multiple_btn)
        btn_layout.addWidget(save_as_btn)
        
        # 信息显示
        self.info_label = QLabel()
        
        layout.addWidget(self.image_label)
        layout.addLayout(btn_layout)
        layout.addWidget(self.info_label)
        
        self.setLayout(layout)
        self.setWindowTitle('图片查看器')
        
    def open_image(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self,
            "打开图片",
            "",
            "图片文件 (*.png *.jpg *.jpeg *.bmp *.gif);;所有文件 (*)"
        )
        
        if file_name:
            self.load_image(file_name)
            
    def open_multiple_images(self):
        file_names, _ = QFileDialog.getOpenFileNames(
            self,
            "打开多张图片",
            "",
            "图片文件 (*.png *.jpg *.jpeg *.bmp *.gif)"
        )
        
        if file_names:
            self.load_image(file_names[0])  # 显示第一张图片
            self.info_label.setText(f"已加载 {len(file_names)} 张图片，当前显示: {file_names[0]}")
            
    def save_image_as(self):
        if not self.current_image:
            QMessageBox.warning(self, "警告", "没有图片可保存")
            return
            
        file_name, _ = QFileDialog.getSaveFileName(
            self,
            "保存图片",
            "image.png",
            "PNG文件 (*.png);;JPEG文件 (*.jpg);;BMP文件 (*.bmp)"
        )
        
        if file_name:
            # 在实际应用中，这里会执行图片保存操作
            self.info_label.setText(f"图片将保存到: {file_name}")
            
    def load_image(self, file_path):
        try:
            pixmap = QPixmap(file_path)
            if not pixmap.isNull():
                # 缩放图片以适应标签大小
                scaled_pixmap = pixmap.scaled(
                    self.image_label.size(),
                    Qt.KeepAspectRatio,
                    Qt.SmoothTransformation
                )
                self.image_label.setPixmap(scaled_pixmap)
                self.current_image = file_path
                self.info_label.setText(f"已加载: {file_path}")
            else:
                QMessageBox.warning(self, "错误", "无法加载图片文件")
        except Exception as e:
            QMessageBox.critical(self, "错误", f"加载图片时出错: {str(e)}")

            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ImageViewer()
    window.show()
    sys.exit(app.exec_())